let main_window =
    GWindow.window
    ~width:800
    ~height:600
    ~title:"Maps Are Pointless"
    ~position:`CENTER
    ~resizable:false ()

let hbox =                  (*conteneur principal*)
    GPack.hbox
    ~border_width:10
    ~spacing:10
    ~packing:main_window#add ()

let tabs =
    GPack.notebook
    ~homogeneous_tabs:false
    ~packing:hbox#add ()

let deuxd_box =
    GPack.hbox ()
let name_deuxdbox = GMisc.label ~text:"2D" ()
let add_deuxdbox =
    tabs#insert_page ~tab_label:name_deuxdbox#coerce deuxd_box#coerce

let bbox =                  (*conteneur des boutons*)
    GButton.toolbar
    ~orientation:`VERTICAL
    ~style:`BOTH
    ~width:150
    ~height:050
    ~border_width:10
    ~packing:(deuxd_box#pack ~expand:false) ()

let imagebox =              (*conteneur de l'image*)
    GPack.hbox
    ~border_width:60
    ~spacing:10
    ~packing:deuxd_box#add ()

let troisdbox =
    GPack.hbox
    ~border_width:10
    ~spacing:10 ()
let name_troisdbox = GMisc.label ~text:"3D" ()
let add_troisdbox =
    tabs#insert_page ~tab_label:name_troisdbox#coerce troisdbox#coerce

let default d = function    (*string_option -> unit*)
    | None -> d
    | Some v -> v

let img = ref "logo.png"

let image =                 (*affiche l'image*)
    GMisc.image
    ~file:!img
    ~packing:imagebox#pack ()

let recup btn () =          (*recupere le nom de l'image a ouvrir*)
    img := default "" (btn#filename);
    Saidalex.src := !img;
    Gaux.may print_endline btn#filename

let open_button =           (*bouton pour parcourir les dossiers*)
    let btn =
    GFile.chooser_button
    ~action:`OPEN
    ~packing:bbox#add () in
    btn#connect#selection_changed
    ~callback:(recup btn);
    btn#connect#selection_changed
    ~callback:(fun () -> image#clear ());
    btn#connect#selection_changed
    ~callback:(fun () -> image#set_file !img);
    btn#connect#selection_changed
    ~callback:(fun () -> Saidalex.rech_ins ());
    btn

let pas = ref (float_of_int !Saidalex.pas)

let get_rgb n = List.nth !Saidalex.liste n
let clr_btn_list = ref []
let spn_btn_list = ref []

let extract_red color =
    if ((Gdk.Color.red color) / 257 = 0) then
        0
    else
        ((Gdk.Color.red color) / 257) + 1
let extract_blue color =
    if ((Gdk.Color.blue color) / 257 = 0) then
        0
    else
        ((Gdk.Color.blue color) / 257) + 1
let extract_green color =
    if ((Gdk.Color.green color) / 257 = 0) then
        0
    else
        ((Gdk.Color.green color) / 257) + 1

let popup () =
    let popup_window =
        GWindow.window
        ~title:"Altitudes"
        ~position:`CENTER
        ~resizable:false () in

        let popup_container =
            GPack.vbox
            ~border_width:10
            ~spacing:10
            ~packing:popup_window#add () in

            let nb_colors = List.length !Saidalex.liste in
            let couleurs =
                for i = 0 to (nb_colors - 1) do
                        let bbox_line_1 =
                            GPack.button_box `HORIZONTAL
                            ~packing:popup_container#add () in

                        let color_button =
                            GButton.color_button
                            ~color:(GDraw.color (`RGB (let (r,g,b)= get_rgb i in
                            r*256,g*256,b*256)))
                            ~packing:bbox_line_1#add () in
                        let height_button =
                            GEdit.spin_button
                            ~snap_to_ticks:true
                            ~rate:1.0
                            ~digits:0
                            ~adjustment:(GData.adjustment ~upper:1500.
                                        ~lower:0. ~page_size:0. ())
                            ~value:(40. *. (float_of_int i))
                            ~update_policy:`ALWAYS
                            ~packing:bbox_line_1#add () in
                        clr_btn_list := color_button::!clr_btn_list;
                        spn_btn_list := height_button::!spn_btn_list;
                done in
            let btn =
                GButton.button
                ~label:"OK"
                ~packing:popup_container#add () in
            btn#connect#clicked
            ~callback:(fun () ->
    Saidalex.liste_hrgb := [];
    let nb_colors = List.length !Saidalex.liste in
    for i = 0 to nb_colors - 1 do
    let spin = List.nth !spn_btn_list i in
    let color_bouton = List.nth !clr_btn_list i in
    let color = color_bouton#color in
    let hauteur = int_of_float spin#value in
    let (r,g,b) = (extract_red color,extract_green color,extract_blue color) in
    Saidalex.liste_hrgb := (hauteur,r,g,b)::!Saidalex.liste_hrgb;
    done);
    btn#connect#clicked
    ~callback:(fun () -> popup_window#misc#hide ());
                    popup_window

let popup_btn =
    let btn =
    GButton.button
    ~label:"Altitudes"
    ~packing:bbox#add () in
    btn#connect#clicked
    ~callback:(fun () ->
        begin
            let w = popup () in
                w#show ()
        end)

let pas_txt = GMisc.label ~text:"\nPas :" ~packing:bbox#add ()

let adjustments =
    GData.adjustment
    ~value:!pas
    ~lower:3.
    ~upper:110.
    ~page_incr:1.
    ~step_incr:5. ()

let scale_button =
    GRange.scale `HORIZONTAL
    ~update_policy:`CONTINUOUS
    ~digits:0
    ~adjustment:adjustments
    ~packing:bbox#add ()

let radio_txt = GMisc.label ~text:"\nEchantillonage :                          " ~packing:bbox#add ()

let radiobox =
    GPack.button_box `VERTICAL
    ~packing:bbox#add ()

let filtres_txt = GMisc.label ~text:"Filtres :                                 " ~packing:bbox#add ()

let check_button_med =
    GButton.check_button
    ~label:"Filtre median"
    ~packing:bbox#add ()

let obj = ["Basique"; "Quadtree"; "Delaunay"]

let build =
      let rec loop group = function
            | [] -> []
            | label :: t ->
            let btn = GButton.radio_button
            ~label ?group ~packing:radiobox#add () in
        btn :: loop (Some (Gaux.default btn#group ~opt:group)) t in
    loop None obj

let check_button_ctr =
    GButton.check_button
    ~label:"Filtre contraste"
    ~packing:bbox#add ()

let hauteurs_window =
    let popup =
        GWindow.window
        ~width:400
        ~height:400
        ~title:"Altitudes"
        ~resizable:false () in
    popup

let btns =
    GPack.vbox
    ~packing:hauteurs_window#add ()

let h_btns =
    GPack.button_box `HORIZONTAL
    ~border_width:070
    ~packing:btns#pack ()

let clr_btn =
    GButton.color_button
    ~color:(GDraw.color (`RGB (255,250,255)))
    ~packing:h_btns#add ()

                let height1_button =
                    GEdit.spin_button
                    ~snap_to_ticks:true
                    ~rate:1.0
                    ~digits:1
                    ~value:1.
                    ~update_policy:`IF_VALID
                    ~packing:h_btns#add ()

let blank_txt = GMisc.label ~text:"" ~packing:bbox#add ()

let save_button =           (*WIP*)
    GButton.button
    ~label:"Traiter"
    ~packing:bbox#add ()

let troisd_button =         (*bouton pour l'affichage 3D*)
    GButton.button
    ~label:"3D"
    ~packing:bbox#add ()

let authors = ["vermor_c"; "beling_a"; "essaki_s"; "boulea_b"]

let about =
    let dlg =
    GWindow.about_dialog
    ~authors:authors
    ~website:"http://maps-are-pointless.eu"
    ~website_label:"Site web"
    ~copyright:"Programme par Colorblind over the world"
    ~comments:"Maps Are Pointless" () in
    let btn = GButton.button
    ~stock:`ABOUT
    ~packing:bbox#add () in
    btn#connect#clicked
    ~callback:(fun () -> ignore(dlg#run ());dlg#misc#hide ())

(*let about_button =
    let btn =
    GButton.button
    ~label:"A propos"
    ~packing:bbox#add () in
    btn#connect#clicked
    ~callback:(fun () ->
        begin
        let popup_window =
            GWindow.window
            ~width:400
            ~height:400
            ~title:"Altitudes"
            ~position:`CENTER
            ~resizable:false () in

        let popup_container =
            GPack.hbox
            ~border_width:10
            ~spacing:10
            ~packing:popup_window#add () in

        let about_text =
            GMisc.label
            ~text:"Coucou"
            ~packing:popup_container#add () in

        popup_window#show ()
        end)*)

let quit_button =           (*Comme le port salut*)
    GButton.button
    ~label:"Quitter"
    ~packing:bbox#add ()

let delete_event ev =       (*Signal de fermeture de fenetre*)
    print_endline "Delete event occured";
    flush stdout;
    false

let destroy () =            (*detruit la fenetre*)
    GMain.Main.quit ()

let quit () =               (*commandes pour quitter factorisees*)
    main_window#event#connect#delete ~callback:delete_event;
    main_window#connect#destroy ~callback:destroy;
    quit_button#connect#clicked ~callback:destroy

let set_pas () =
    adjustments#connect#value_changed
    ~callback:(fun () -> pas := adjustments#value)

let filtres ()=
Saidalex.filtre := 0;
    if (check_button_med#active) then
        begin
            Saidalex.filtre := (!Saidalex.filtre + 1)
        end;
    if (check_button_ctr#active) then
        begin
            Saidalex.filtre := (!Saidalex.filtre + 2)
        end

let makeobj () =
    if ((List.nth build 0)#active) then
        begin
            Saidalex.triangulation := 1
        end;
    if ((List.nth build 1)#active) then
        begin
            Saidalex.triangulation := 3
        end;
    if ((List.nth build 2)#active) then
        begin
            Saidalex.triangulation := 2
        end

let click_troisd () =
    troisd_button#connect#clicked
    ~callback:(fun () -> Saidalex.quadrillage_ou_point_obj := 1);
    troisd_button#connect#clicked
    ~callback:(Saidalex.main);
    troisd_button#connect#clicked
    ~callback:(fun () -> Testgl.drawNewFile "triangulation.obj"
    !Saidalex.liste_hrgb);
    troisd_button#connect#clicked
    ~callback:(fun () -> tabs#next_page ())

let save () =
    save_button#connect#clicked
    ~callback:(fun () -> Saidalex.quadrillage_ou_point_obj := 0);
    save_button#connect#clicked
    ~callback:(fun () -> Saidalex.pas := int_of_float(!pas));
    save_button#connect#clicked
    ~callback:(fun () -> filtres ());
    save_button#connect#clicked
    ~callback:(fun () -> makeobj ());
    save_button#connect#clicked
    ~callback:(Saidalex.main);
    save_button#connect#clicked
    ~callback:(fun () -> image#clear ());
    save_button#connect#clicked
    ~callback:(fun () -> (img := "map_final.bmp"));
    save_button#connect#clicked
    ~callback:(fun () -> image#set_file !img)

let main () =
    Param.main();
    main_window#show ();
    set_pas();
    click_troisd ();
    save ();
    quit ();
    Testgl.main troisdbox main_window;
    GMain.Main.main ()

let _ = main ()
